const fileService = require("../service/file.server");
const userService = require("../service/user.server");
// const { AVATAR_PATH } = require("../constants/file-path");
const { APP_HOST, APP_PORT } = require("../config/app");

class FileController {
  async saveAvatarInfo(ctx, next) {
    // // 1.获取图像相关的信息 单张图片file，多张files
    const { filename, mimetype, size } = ctx.request.file;
    const { id } = ctx.user;

    // // 2.将图像信息数据保存到数据库中
    const result = await fileService.createAvatar(filename, mimetype, size, id);
    console.log((APP_HOST, APP_PORT, "888"));

    // // 3.将图片地址保存到user表中
    const avatarUrl = `${APP_HOST}:${APP_PORT}/users/${id}/avatar`;
    await userService.updateAvatarUrlById(avatarUrl, id);

    // 4.返回结果
    ctx.body = {
      code: 200,
      message: "头像上传成功",
      data: result,
    };
  }

  // async savePictureInfo(ctx, next) {
  //   // 1.获取图像信息
  //   const files = ctx.req.files;
  //   const { id } = ctx.user;
  //   const { momentId } = ctx.query;

  //   // 2.将所有的文件信息保存到数据库中
  //   for (let file of files) {
  //     const { filename, mimetype, size } = file;
  //     await fileService.createFile(filename, mimetype, size, id, momentId);
  //   }

  //   ctx.body = '动态配图上传完成~'
  // }
}

module.exports = new FileController();
